package com.doraro.coal.moudle.user.Interceptor;

import com.doraro.coal.moudle.user.model.entity.User;
import com.doraro.coal.utils.ShiroUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by Wuzhanghang on 2019/9/26 16:56
 */

@Component
public class FirstLoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = ShiroUtils.getUserEntity();
        //判断用户是否为初次登陆，若是则跳转至修改密码页面
        if(user!=null && user.getStatus()== 2){
            request.getRequestDispatcher("/person").forward(request,response);
            return false;
        }
        //已登录不能跳到登录页
        final String uri = request.getRequestURI();
        if(user!=null && ("/".equals(uri) || "/anon/index".equals(uri))){
            request.getRequestDispatcher("/sys/main").forward(request,response);
            return false;
        }
        return true;

    }
}
